﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.ISV.RCV.Proxy;
    using UFIDA.U9.PM.Rcv;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// RCVSaveSV partial 
	/// </summary>	
	public partial class RCVSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new RCVSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class RCVSaveSVImpementStrategy : BaseStrategy
	{
		public RCVSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			RCVSaveSV bpObj = (RCVSaveSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

				List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
					, typeof(UFIDA.U9.ISV.RCV.DTO.OBAReceivementDTOData));
				foreach (UFIDA.U9.ISV.RCV.DTO.OBAReceivementDTOData dto in inputdtos)
				{
					Receivement item = Receivement.Finder.Find($"DocNo='{dto.DocNo}'");
					if (item == null)
					{
						//新增逻辑
						CreateRCVSRVProxy proxy = new CreateRCVSRVProxy();
						proxy.RCVList = new List<UFIDA.U9.ISV.RCV.DTO.OBAReceivementDTOData> { dto };
                        List<ReceivementData> ret = proxy.Do();
						//修改单号并返回
						retlist.Add(new CommonArchiveDataDTOData
						{
							ID = ret[0].ID,
							Code = EntityUtils.UpdateDocNo<Receivement>(ret[0].ID, dto.DocNo)
						});
						//审核
						RCVApproveSV approve = new RCVApproveSV();
						approve.Data = new List<CopyOfCommonArchiveDataDTOData>
						{
							new CopyOfCommonArchiveDataDTOData
							{
								Code =dto.DocNo,
							}
						};
                        ResultDTOData resultDTOData = approve.Do();
						ResultUtils.CheckResult(resultDTOData);
					}
					else
					{
						//修改逻辑
						throw new AITException(599,$"单号:{dto.DocNo}已经存在,不能生成!");
					}
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}		
	}

	#endregion
	
	
}